image_name         := my-debian

container_name     := my-debian
container_username := debian
container_useruid  := $(shell id -u)

session_dbus_dir := $(shell echo $$DBUS_SESSION_BUS_ADDRESS | sed 's/unix:path=\(.*\),.*/\1/' | xargs dirname)

app := $(value app)


debug:
	@echo session_dbus_dir = $(session_dbus_dir)


build: rm-image
	docker build . -t $(image_name):latest --network host \
	--build-arg "NAME=$(container_username)" \
	--build-arg "UID=$(container_useruid)"


rm-container:
	-@docker kill $(container_name) &> /dev/null || true
	-@docker rm $(container_name) &> /dev/null || true


rm-image: rm-container
	-@docker rmi $(image_name) &> /dev/null || true


rm-volume: rm-container
	-@docker volume rm $(container_name)-home &> /dev/null || true


rm-all: rm-container rm-image rm-volume


volume:
	@if [[ -z `docker volume ls | grep $(container_name)-home` ]];then \
	echo -e "\e[1;32mcreating volume\e[0m"; \
	docker volume create $(container_name)-home; \
	fi


container: volume
	@if [[ -z `docker container ls -a -f name=$(container_name)|grep $(container_name)` ]];then \
	echo -e "\e[1;32mcreating container\e[0m"; \
	docker create \
	-it \
	--name $(container_name) \
	--privileged \
	--network host \
	-e DISPLAY=$$DISPLAY \
	-e GDK_SCALE=2 \
	-e GDK_DPI_SCALE=0.5 \
	-e XMODIFIERS=@im=fcitx \
	-e QT_IM_MODULE=fcitx \
	-e GTK_IM_MODULE=fcitx \
	-e QT_QPA_PLATFORMTHEME=qt5ct \
	-e XDG_RUNTIME_DIR=$$XDG_RUNTIME_DIR \
	-v $$XDG_RUNTIME_DIR:$$XDG_RUNTIME_DIR \
	-v /dev:/dev \
	-v /run/dbus:/run/dbus \
	-v /etc/machine-id:/etc/machine-id \
	-v $(session_dbus_dir):$(session_dbus_dir) \
	-v /tmp/.X11-unix:/tmp/.X11-unix \
	-v $(container_name)-home:/home/$(container_username):rw \
	-v $$HOME/.bashrc:/home/$(container_username)/.bashrc:ro \
	-v $$HOME/.fonts:/home/$(container_username)/.fonts:ro \
	-v $$HOME/.icons:/home/$(container_username)/.icons:ro \
	-v $$HOME/.themes:/home/$(container_username)/.themes:ro \
	-v $$HOME/.gtkrc-2.0:/home/$(container_username)/.gtkrc-2.0:ro \
	-v $$HOME/.config/gtk-3.0:/home/$(container_username)/.config/gtk-3.0:ro \
	-v $$HOME/.config/gtk-2.0:/home/$(container_username)/.config/gtk-2.0:ro \
	-v $$HOME/.config/awesome/icons/:/home/$(container_username)/.config/awesome/icons/:rw \
	-v $$HOME/Documents:/home/$(container_username)/Documents:rw \
	-v $$HOME/Downloads:/home/$(container_username)/Downloads:rw \
	-v $$HOME/Github:/home/$(container_username)/Github:rw \
	-v $$HOME/Music:/home/$(container_username)/Music:rw \
	-v $$HOME/Pictures:/home/$(container_username)/Pictures:rw \
	-v $$HOME/Scripts:/home/$(container_username)/Scripts:rw \
	-v $$HOME/Shared:/home/$(container_username)/Shared:rw \
	-v $$HOME/Videos:/home/$(container_username)/Videos:rw \
	-v $$HOME/Work:/home/$(container_username)/Work:rw \
	$(image_name); \
	fi


start: container
	@if [[ -z `docker container ls -f name=$(container_name)|grep Up` ]];then \
	echo -e "\e[1;32mstarting container\e[0m"; \
	xhost + &> /dev/null; \
	docker container start $(container_name); \
	fi


bash: start
	@echo -e "\e[1;32mentering container\e[0m"
	@docker exec -it -e DBUS_SESSION_BUS_ADDRESS=$$DBUS_SESSION_BUS_ADDRESS $(container_name) bash || true


run: start
	@docker exec -d -e DBUS_SESSION_BUS_ADDRESS=$$DBUS_SESSION_BUS_ADDRESS $(container_name) $(app) || true
